package com.zjw.zy.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import com.zjw.zy.R;
import com.zjw.zy.smi.ICopy;
import com.zjw.zy.utils.ResourcesUtils;

import java.util.Date;
import java.util.Objects;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/30 8:53
 */
@Entity(tableName = "memorandum")
public final class MemorandumItem implements Parcelable, ICopy<MemorandumItem> {

    @PrimaryKey(autoGenerate = true) // 声明主键，可以选择自增属性
    @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER) //对应列名，及其类型
    public int id;

    @ColumnInfo(name = "date", typeAffinity = ColumnInfo.INTEGER) //对应列名，及其类型
    public long date;

    @ColumnInfo(name = "title", typeAffinity = ColumnInfo.TEXT) //对应列名，及其类型
    public String title;

    @ColumnInfo(name = "text", typeAffinity = ColumnInfo.TEXT) //对应列名，及其类型
    public String text;

    @ColumnInfo(name = "image", typeAffinity = ColumnInfo.TEXT) //对应列名，及其类型
    public String image;

    public MemorandumItem() {

    }

    @Ignore
    public MemorandumItem(int id, long date, String title, String text, String image) {
        this.id = id;
        this.date = date;
        this.title = title;
        this.text = text;
        this.image = image;
    }

    @Ignore
    public MemorandumItem(long date, String title, String text, String image) {
        this.id = 0;
        this.date = date;
        this.title = title;
        this.text = text;
        this.image = image;
    }

    @Ignore
    public MemorandumItem(MemorandumItem other) {
        this.id = other.id;
        this.date = other.date;
        this.title = other.title;
        this.text = other.text;
        this.image = other.image;
    }


    protected MemorandumItem(Parcel in) {
        id = in.readInt();
        date = in.readLong();
        title = in.readString();
        text = in.readString();
        image = in.readString();
    }

    public static final Creator<MemorandumItem> CREATOR = new Creator<MemorandumItem>() {
        @Override
        public MemorandumItem createFromParcel(Parcel in) {
            return new MemorandumItem(in);
        }

        @Override
        public MemorandumItem[] newArray(int size) {
            return new MemorandumItem[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MemorandumItem that = (MemorandumItem) o;
        return id == that.id && Objects.equals(date, that.date)
                && Objects.equals(title, that.title)
                && Objects.equals(text, that.text)
                && Objects.equals(image, that.image);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, date, title, text, image);
    }

    @Override
    public String toString() {
        return "MemorandumItem{" +
                "id=" + id +
                ", date=" + date +
                ", title='" + title + '\'' +
                ", text='" + text + '\'' +
                ", image='" + image + '\'' +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeLong(date);
        dest.writeString(title);
        dest.writeString(text);
        dest.writeString(image);
    }

    public boolean isTitleEmpty() {
        return title == null || title.isEmpty() || title.trim().isEmpty();
    }

    public boolean isTextEmpty() {
        return text == null || text.isEmpty() || text.trim().isEmpty();
    }

    public boolean isEmpty() {
        return isTitleEmpty() && isTextEmpty();
    }

    @NonNull
    @Override
    public MemorandumItem copy() {
        return new MemorandumItem(id, date, title, text, image);
    }
}
